﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;

namespace ASPNETVN.PORTAL.Modules.SECURITY.Controls
{
    public partial class AddEditUser : AddEditItemControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                if (this.ItemID > 0)
                {
                    User user = User.Load(this.ItemID);
                    txtName.Text = user.Name;
                    rbGenderMale.Checked = user.Gender;
                    rbGenderFemale.Checked = !user.Gender;
                    txtEmail.Text = user.Email;

                    txtEmail.CssClass = "ReadOnlyTextBox";
                    txtEmail.ReadOnly = true;

                    txtAddress.Text = user.Address;
                    txtPhone.Text = user.Phone;
                    ddlCountry.SelectedValue = user.Country;
                    ddlCity.SelectedValue = user.City;

                    ddlStatus.SelectedValue = user.Status;

                    // Hidden fields.
                    txtPassword.Value = user.Password;
                    txtJoinedDate.Value = user.JoinedTime.ToString();
                }
                else
                {
                    txtEmail.CssClass = "TextBox";
                    txtEmail.ReadOnly = false;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                User user = new User();
                user.PortalID = Global.PortalID;
                user.Name = txtName.Text;
                user.Gender = rbGenderMale.Checked;
                user.Email = txtEmail.Text;
                user.Address = txtAddress.Text;
                user.Phone = txtPhone.Text;
                user.Country = ddlCountry.SelectedValue;
                user.City = ddlCity.SelectedValue;
                user.Status = ddlStatus.SelectedValue;

                // Hidden fields.
                user.Password = txtPassword.Value;

                if (this.ItemID == 0)
                {
                    user.JoinedTime = DateTime.Now;

                    // Mật khẩu mặc định: p@ssw0rd.
                    user.Password = PortalSecurity.Encrypt("p@ssw0rd");
                    // Kiểm tra email này đã tồn tại hay chưa?
                    if (User.CheckExist(txtEmail.Text.Trim()))
                    {
                        lblMessage.Text = "Email này đã đăng ký.";
                        return;
                    }
                    int userID = user.Insert();
                    // Refresh.
                    string url = HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, userID, "AddEditUser", "");
                    Response.Redirect(url);
                }
                else
                {
                    user.ID = this.ItemID;
                    user.JoinedTime = DateTime.Parse(txtJoinedDate.Value);
                    user.Update();
                }
            }
        }

        protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCountry.SelectedValue.Equals("VN"))
            {
                ddlCity.Visible = true;
                txtCity.Visible = false;
            }
            else
            {
                ddlCity.Visible = false;
                txtCity.Visible = true;
            }
        }

        protected void btnAssignRole_Click(object sender, ImageClickEventArgs e)
        {
            User.AddToRole(this.ItemID, Int32.Parse(ddlRole.SelectedValue));
            gvUserRole.DataBind();
            ddlRole.DataBind();
        }

        protected void gvUserRole_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "RemoveRole")
            {
                User.RemoveRole(this.ItemID, Int32.Parse(e.CommandArgument.ToString()));
                gvUserRole.DataBind();
                ddlRole.DataBind();
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, 0, ModulePages.Users));
        }

        protected void btnResetPassword_Click(object sender, EventArgs e)
        {
            User.ResetPassword(txtEmail.Text, txtNewPassword.Text);
            this.ShowMessage("Thông báo", "Cấp lại mật khẩu thành công!");
        }
    }
}